twitter agents can be disabled too

Andrew Cantino лет %!s(int64=10): %!d(string=назад)
Родитель
Сommit
fdf3565199

+ 5 - 3
Gemfile

@@ -15,6 +15,11 @@ gem "google-api-client"           # GoogleCalendarPublishAgent
15 15
 gem 'mqtt'                        # MQTTAgent
16 16
 gem 'slack-notifier', '~> 0.5.0'  # SlackAgent
17 17
 
18
+# Twitter Agents
19
+gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
20
+gem 'omniauth-twitter'
21
+gem 'twitter', '~> 5.8.0'
22
+
18 23
 # Optional Services.
19 24
 gem 'omniauth-37signals'          # BasecampAgent
20 25
 # gem 'omniauth-github'
@@ -31,7 +36,6 @@ gem 'protected_attributes', '~>1.0.8' # This must be loaded before some other ge
31 36
 gem 'ace-rails-ap', '~> 2.0.1'
32 37
 gem 'bootstrap-kaminari-views', '~> 0.0.3'
33 38
 gem 'bundler', '>= 1.5.0'
34
-gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
35 39
 gem 'coffee-rails', '~> 4.0.0'
36 40
 gem 'daemons', '~> 1.1.9'
37 41
 gem 'delayed_job', '~> 4.0.0'
@@ -57,14 +61,12 @@ gem 'mysql2', '~> 0.3.16'
57 61
 gem 'multi_xml'
58 62
 gem 'nokogiri', '~> 1.6.1'
59 63
 gem 'omniauth'
60
-gem 'omniauth-twitter'
61 64
 gem 'rails' , '4.1.5'
62 65
 gem 'rufus-scheduler', '~> 3.0.8', require: false
63 66
 gem 'sass-rails',   '~> 4.0.0'
64 67
 gem 'select2-rails', '~> 3.5.4'
65 68
 gem 'spectrum-rails'
66 69
 gem 'therubyracer', '~> 0.12.1'
67
-gem 'twitter', '~> 5.8.0'
68 70
 gem 'typhoeus', '~> 0.6.3'
69 71
 gem 'uglifier', '>= 1.3.0'
70 72
 

+ 8 - 0
app/concerns/twitter_concern.rb

@@ -6,6 +6,8 @@ module TwitterConcern
6 6
 
7 7
     validate :validate_twitter_options
8 8
     valid_oauth_providers 'twitter'
9
+
10
+    gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
9 11
   end
10 12
 
11 13
   def validate_twitter_options
@@ -41,4 +43,10 @@ module TwitterConcern
41 43
       config.access_token_secret = twitter_oauth_token_secret
42 44
     end
43 45
   end
46
+
47
+  module ClassMethods
48
+    def twitter_dependencies_missing
49
+      "## Include the `twitter`, `omniauth-twitter`, and `cantino-twitter-stream` gems in your Gemfile to use Twitter Agents."
50
+    end
51
+  end
44 52
 end

+ 1 - 2
app/models/agents/twitter_publish_agent.rb

@@ -1,5 +1,3 @@
1
-require "twitter"
2
-
3 1
 module Agents
4 2
   class TwitterPublishAgent < Agent
5 3
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
7 5
     cannot_be_scheduled!
8 6
 
9 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
10 9
       The TwitterPublishAgent publishes tweets from the events it receives.
11 10
 
12 11
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
app/models/agents/twitter_stream_agent.rb

@@ -5,6 +5,7 @@ module Agents
5 5
     cannot_receive_events!
6 6
 
7 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
8 9
       The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
9 10
 
10 11
       To follow the Twitter stream, provide an array of `filters`.  Multiple words in a filter must all show up in a tweet, but are independent of order.

+ 1 - 2
app/models/agents/twitter_user_agent.rb

@@ -1,5 +1,3 @@
1
-require "twitter"
2
-
3 1
 module Agents
4 2
   class TwitterUserAgent < Agent
5 3
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
7 5
     cannot_receive_events!
8 6
 
9 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
10 9
       The TwitterUserAgent follows the timeline of a specified Twitter user.
11 10
 
12 11
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
bin/threaded.rb

@@ -14,6 +14,7 @@ def safely(&block)
14 14
   rescue StandardError => e
15 15
     STDERR.puts "\nException #{e.message}:\n#{e.backtrace.join("\n")}\n\n"
16 16
     STDERR.puts "Terminating myself ..."
17
+    STDERR.flush
17 18
     stop
18 19
   end
19 20
 end

+ 8 - 1
lib/twitter_stream.rb

@@ -1,6 +1,5 @@
1 1
 require 'cgi'
2 2
 require 'json'
3
-require 'twitter/json_stream'
4 3
 require 'em-http-request'
5 4
 require 'pp'
6 5
 
@@ -88,6 +87,14 @@ class TwitterStream
88 87
   SEPARATOR = /[^\w_\-]+/
89 88
 
90 89
   def run
90
+    if Agents::TwitterStreamAgent.dependencies_missing?
91
+      STDERR.puts Agents::TwitterStreamAgent.twitter_dependencies_missing
92
+      STDERR.flush
93
+      return
94
+    end
95
+
96
+    require 'twitter/json_stream'
97
+
91 98
     while @running
92 99
       begin
93 100
         agents = Agents::TwitterStreamAgent.active.all